import {createRouter,createWebHashHistory} from 'vue-router';
import {useUserStore} from '@/store/modules/user.js';

const BasicRoutes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    name: 'Login',
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录页',
    },
  },
  {
    name: 'Home',
    path: '/home',
    component: () => import('@/views/home/index.vue'),
    meta: {
      title: '首页',
    },
    children:[
      {
        path:'index',
        name:'0',
        component:()=>import('@/views/index.vue'),
        meta:{roles:['admin_root','admin','maintainer','reviewer']}
      },
      {
        path:'person',
        name:'0-1',
        component:()=>import('@/views/person/index.vue'),
        meta:{roles:['admin_root','admin','maintainer','reviewer']}
      },
      {
        path:'adminInfo',
        name:'1-1',
        component:()=>import('@/views/account/adminInfo.vue'),
        meta:{roles:['admin_root']}
      },
      {
        path:'userInfo',
        name:'1-2',
        component:()=>import('@/views/account/userInfo.vue'),
        meta:{roles:['admin_root','admin']}
      },
      {
        path:'mtvInfo',
        name:'2-1',
        component:()=>import('@/views/content/mtvInfo.vue'),
        meta:{roles:['admin_root','admin']}
      },
      {
        path:'commentInfo',
        name:'2-3',
        component:()=>import('@/views/content/commentInfo.vue'),
        meta:{roles:['admin_root','admin','reviewer']}
      },
      {
        path:'baseInfo',
        name:'3-1',
        component:()=>import('@/views/website/baseInfo.vue'),
        meta:{roles:['admin_root','admin','maintainer']}
      },
      {
        path:'bulletinInfo',
        name:'3-3',
        component:()=>import('@/views/website/bulletinInfo.vue'),
        meta:{roles:['admin_root','admin','maintainer']}
      },
      {
        path:'recommendInfo',
        name:'3-2',
        component:()=>import('@/views/website/recommendInfo.vue'),
        meta:{roles:['admin_root','admin','maintainer']}
      }
    ]
  },
  {
    name:'Person',
    path:'/person',
    component:()=>import('@/views/person/index.vue'),
    meta:{
      title:'个人中心',
    }
  },
  {
    name: '404',
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面飞走了',
    },
  },
  {
    name: '403',
    path: '/403',
    component: () => import('@/views/error/403.vue'),
    meta: {
      title: '没有权限',
    },
  },
]

// 创建Vue Router实例并添加全局前置守卫
const router = createRouter({
  history: createWebHashHistory(),
  routes: BasicRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});
router.beforeEach(async (to, from, next) => {  
  const store = useUserStore();  
  
  if (store.userInfo !== null) {  
    // 如果访问的是403页面，则不进行任何操作  
    if (to.name === '403' || to.name === '404' || to.name === 'Login' || to.name === 'Home' || to.name === 'Person') {  
      next();
      return;  
    }   
    // 检查用户角色是否具有访问当前路由的权限
    const hasPermission = store.auth.includes(to.name); // 根据角色映射表判断该路由是否允许当前角色访问  
    if (hasPermission) {  
      next();  
    } else {
      //区分是403还是404
      if(to.name)  
      next('/403'); // 跳转到无权限页面或其他默认页面
      else
      next('/404'); 
    }  
  } else {  
    // 如果当前路由已经是Login，则不再重定向  
    if (to.name === 'Login') {  
      next();  
    } else {  
      next({ name: 'Login' });  
    }  
  }  
});

export default router;
